//
//  SMTabBarView.swift
//  SomePasti
//
//  Created by 李永彬 on 2025/10/7.
//

import SwiftUI
import SwiftUIIntrospect

let mainNav = "main"
struct SMTabBarView: View {
    @State private var selectedTab: Int = 0
    @State private var isTabBarHidden: Bool = false // 控制显示/隐藏
    
    private let tabItems: [SMTabItem] = [
        SMTabItem(title: localizedString("Home"), icon: nil, color: Color(hex: "#FFFFFF")),
        SMTabItem(title: localizedString("Mine"), icon: nil, color: Color(hex: "#FFFFFF")),
    ]
    
    var body: some View {
        NavigationView {
            ZStack(alignment: .bottom) {
                // 内容区域
                TabView(selection: $selectedTab) {
                    SMHomeView()
                        .tag(0)
                        .navigationViewManager(for: "home", afterBackDo: {
                            print("已返回到根视图")
                        })
                    SMMineView()
                        .tag(1)
                        .navigationViewManager(for: "home", afterBackDo: {
                            print("已返回到根视图")
                        })
                }
                
                // 根据状态立即显示/隐藏，无动画
                ZStack(alignment: .bottom) {
                    (selectedTab == 0) ?
                    Image("tabbar_item1")
                        .resizable()
                        .scaledToFill()
                        .edgesIgnoringSafeArea(.all) :
                    Image("tabbar_item2")
                        .resizable()
                        .scaledToFill()
                        .edgesIgnoringSafeArea(.all)
                    
                    // 自定义TabBar
                    HStack(spacing: 0) {
                        ForEach(0..<tabItems.count, id: \.self) { index in
                            SMTabButton(
                                item: tabItems[index],
                                isSelected: selectedTab == index
                            ) {
                                selectedTab = index
                            }
                            .frame(maxWidth: .infinity)
                        }
                    }
                }
                .frame(height: AppConstants.tabBarHeight)
                .background(Color.clear)
                .accessibilityHidden(isTabBarHidden)
            }
            .edgesIgnoringSafeArea(.bottom)
            .onAppear {
                // 监听通知
                setupNotificationObserver()
            }
            .onDisappear {
                // 移除监听
                NotificationCenter.default.removeObserver(self)
            }
        }
        .navigationViewManager(for: mainNav, afterBackDo: {
            print("返回到tabBar")
        })
        .introspect(.navigationView(style: .stack), on: .iOS(.v13, .v14, .v15, .v16, .v17, .v18, .v26)) { navigationController in
            // 关键：存储导航控制器
            NavigationControllerStore.shared.storeNavigationController(navigationController, for: mainNav)
        }
    }
    
    // 设置通知监听
    private func setupNotificationObserver() {
        NotificationCenter.default.addObserver(
            forName: .updateTabBarVisibility,
            object: nil,
            queue: .main
        ) { notification in
            // 立即更新状态，无延迟
            if let isHidden = notification.userInfo?[TabBarVisibilityKeys.isHidden] as? Bool {
                self.isTabBarHidden = isHidden
            }
        }
    }
}

struct SMTabItem {
    let title: String
    let icon: String?
    let color: Color
}

// 预览
struct SMTabBarView_Previews: PreviewProvider {
    static var previews: some View {
        SMTabBarView()
    }
}
